package com.vvlive.caibao.smartcity.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.vvlive.caibao.smartcity.bean.EventCard;
import com.vvlive.caibao.smartcity.utils.ScreenUtil;

import java.util.List;

import smartcity.vvlive.caibao.com.smartc.R;

/**
 * Created by zhj on 2015/10/12.
 */
public class HoursView extends ScrollView {

    /**
     * How many hours of one day
     */
    private static final int HOURS_COUNT = 24;
    /**
     * The default value of the event card's margin-left
     */
    private static final int CARD_MARGIN_LEFT = 32;

    private static final int INDEX_TITLE = 0;
    private static final int INDEX_SECOND_TITLE = 1;
    private static final int INDEX_DESCRIPTION = 2;

    private Context mContext;
    private FrameLayout mWrapper;
    private LinearLayout mHoursWrapper;
    private EventCardView mEventCardView;
    private ScreenUtil mScreen;
    private HourClickListener mHourClickListener;
    private OnHourClickListener onHourClickListener;

    public HoursView(Context context) {
        this(context, (AttributeSet) null);
    }

    public HoursView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        mScreen = new ScreenUtil(context);
        mHourClickListener = new HourClickListener();
        initViews();
    }

    private void initViews() {
        mWrapper = inflateView(R.layout.view_hours_view_scroller);
        mHoursWrapper = find(mWrapper, R.id.hours_wrapper);
        mEventCardView = find(mWrapper, R.id.meetings_wrapper);
        mEventCardView.setTranslationX(mScreen.dip2px(CARD_MARGIN_LEFT));

        for (int i = 0; i < HOURS_COUNT; i++) {
            View view = inflateView(R.layout.view_hours_view_item);
            ((TextView) view.findViewById(R.id.hours_view_hour)).setText(i + "");
            View firstHalfView = view.findViewById(R.id.hours_view_first_half);
            firstHalfView.setTag(i);
            firstHalfView.setOnClickListener(mHourClickListener);
            View restHalfView = view.findViewById(R.id.hours_view_rest_half);
            restHalfView.setTag(i);
            restHalfView.setOnClickListener(mHourClickListener);
            mHoursWrapper.addView(view);
        }

        this.addView(mWrapper);
    }

    final class HourClickListener implements OnClickListener{

        @Override
        public void onClick(View v) {
            boolean isHalf = true;
            if(v.getId() == R.id.hours_view_rest_half){
                isHalf = false;
            }
            if(onHourClickListener != null){
                onHourClickListener.onHourClick((int) v.getTag(), isHalf);
            }

        }
    }

    /**
     * Add one event card
     *
     * @param eventCard
     */
    public void addEventCard(EventCard eventCard) {
        mEventCardView.addEvent(eventCard);
    }

    /**
     * Add more than one event card
     *
     * @param eventCards
     */
    public void setEventCards(List<EventCard> eventCards) {
        mEventCardView.setEvents(eventCards);
    }

    public void setOnEventClickListener(EventCardView.OnEventClickListener onEventClickListener){
        if(onEventClickListener != null) {
            mEventCardView.setOnEventClickListener(onEventClickListener);
        }
    }

    public void setOnEventLongClickListener(EventCardView.OnEventLongClickListener onEventLongClickListener){
        if(onEventLongClickListener != null){
            mEventCardView.setOnEventLongClickListener(onEventLongClickListener);
        }
    }

    public interface OnHourClickListener {
        void onHourClick(int hour, boolean isHalf);
    }

    /**
     * 设置时间的点击事件
     *
     * @param onHourClickListener
     */
    public void setOnHourClickListener(OnHourClickListener onHourClickListener) {
        this.onHourClickListener = onHourClickListener;
    }

    private <T extends View> T inflateView(int res) {
        return (T) inflate(mContext, res, null);
    }

    private <T extends View> T find(View parent, int id) {
        return (T) parent.findViewById(id);
    }


}
